package labb2.del2;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Books - created by a title, isbn-number, price and title. Unlimited number of
 * authors can be added to the book.
 *
 * @author Mikko Puustinen
 * @version 0.1, 2007-sep-26
 * @see Author
 */
public class Book implements Comparable<Book>, Serializable {
    private ArrayList<Author> authors;
    private String title;
    private String isbn;
    private int edition;
    private double price;

    /**
     * Constructs a Book.
     *
     * @param title   title
     * @param isbn    ISBN
     * @param edition edition
     * @param price   price
     */
    public Book(String title, String isbn, int edition, double price) {
        this.title = title;
        this.isbn = isbn;
        this.edition = edition;
        this.price = price;
        authors = new ArrayList<Author>();
    }

    /**
     * @param title   title
     * @param isbn    ISBN
     * @param edition edition
     * @param price   price
     * @param authors array consisting of author names.
     */
    public Book(String title, String isbn, int edition, double price, String[] authors) {
        this(title, isbn, edition, price);
        for (String str : authors)
            addAuthor(str);
    }

    /**
     * Adds an <code>Author</code> to the Book.
     *
     * @param author author
     */
    public void addAuthor(Author author) {
        authors.add(author);
    }

    /**
     * Adds and creates a new <code>Author</code>.
     *
     * @param name of the Author.
     */
    public void addAuthor(String name) {
        authors.add(new Author(name));
    }

    /**
     * Returns a copy of the <code>ArrayList</code> containing all the
     * <code>Author</code>s of the <code>Book</code>.
     *
     * @return all Authors.
     */
    public ArrayList<Author> getAuthors() {
        ArrayList<Author> clone = new ArrayList<Author>();
        clone.addAll(authors);
        return clone;
    }

    /**
     * Returns the ISBN-number.
     *
     * @return ISBN
     */
    public String getISBN() {
        return isbn;
    }

    /**
     * Returns the title.
     *
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Get the edition of the Book.
     *
     * @return the edition of the Book.
     */
    public int getEdition() {
        return edition;
    }

    /**
     * Get the price of the Book.
     *
     * @return the price when the Book was bought.
     */
    public double getPrice() {
        return price;
    }

    /**
     * Compares this object with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     *
     * @param b the Book to be compared.
     *
     * @return a negative integer, zero, or a positive integer as this object is
     *         less than, equal to, or greater than the specified object.
     */
    public int compareTo(Book b) {
        return this.title.compareToIgnoreCase(b.getTitle());
    }
}

